from dotenv import load_dotenv
import os
import pathlib

# 获取app目录的绝对路径
app_dir = pathlib.Path(__file__).parent.parent.absolute()

# 根据环境变量加载对应的 .env 文件
env = os.getenv('ENV', 'dev')  # 默认加载开发环境
env_file = app_dir / f".env.{env}"
load_dotenv(dotenv_path=env_file)

# 根据平台设置Redis配置
REDIS_HOST = os.getenv('REDIS_HOST')
REDIS_PORT = int(os.getenv('REDIS_PORT', 6379))  # 添加类型转换并设置默认值
REDIS_PASSWORD = os.getenv('REDIS_PASSWORD')
REDIS_DB_BROKER = int(os.getenv('REDIS_DB_BROKER', 0))  # 添加类型转换并设置默认值
REDIS_DB_BACKEND = int(os.getenv('REDIS_DB_BACKEND', 1))  # 添加类型转换并设置默认值

broker_url = f'redis://:{REDIS_PASSWORD}@{REDIS_HOST}:{REDIS_PORT}/{REDIS_DB_BROKER}'
result_backend = f'redis://:{REDIS_PASSWORD}@{REDIS_HOST}:{REDIS_PORT}/{REDIS_DB_BACKEND}'
print(broker_url,"连接的redis地址")
print(result_backend,"返回的redis地址")
# 通用Celery配置
timezone = 'Asia/Shanghai'

# Windows特定配置
worker_pool_restarts = True
worker_max_tasks_per_child = 1
pool_options = {'processes': 1}
broker_connection_retry_on_startup = True

# 任务配置
task_serializer = 'json'
result_serializer = 'json'
accept_content = ['json']
task_track_started = True
task_time_limit = 3600
result_expires = 3600